/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ForeignKeyDialog.java
 *
 * Created on Jun 30, 2009, 3:43:07 PM
 */

package windows;

import etc.Database;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.table.TableModel;

/**
 *
 * @author zoli
 */
public class ForeignKeyDialog extends javax.swing.JDialog {

    private Vector fkdatatable;
    private Database d;
   


    public void setColumns(TableModel model)
    {
        for (int i=0;i<model.getRowCount();i++)
        {
            Vector v=new Vector();
            for (int j=0;j<model.getColumnCount();j++)
            {
                v.add(model.getValueAt(i, j));
            }
            this.fkdatatable.add(v);
        }
    }

    private void fkListRefresh()
    {
        
    }



    /** Creates new form ForeignKeyDialog */
    public ForeignKeyDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.fkdatatable=new Vector();
        this.d=new Database();
        this.d=d.getInstance();
        d.connect();

        


        
       
        this.referencedCombo.setModel(new DefaultComboBoxModel(d.getTableNames()));
        

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        fkList = new javax.swing.JList();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        keyNameField = new javax.swing.JTextField();
        onDeleteCombo = new javax.swing.JComboBox();
        onUpdateCombo = new javax.swing.JComboBox();
        columnCombo = new javax.swing.JComboBox();
        referencedCombo = new javax.swing.JComboBox();
        refenrecedColumnCombo = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Foreign Keys");

        jButton1.setText("OK");

        jButton2.setText("Cancel");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        fkList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                fkListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(fkList);

        jButton3.setText("+");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("-");

        jLabel1.setText("Key Name:");

        jLabel2.setText("ON DELETE:");

        jLabel3.setText("ON UPDATE:");

        jLabel4.setText("Referenced Table:");

        jLabel5.setText("Column from current table:");

        jLabel6.setText("Column from referenced table:");

        keyNameField.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                keyNameFieldCaretUpdate(evt);
            }
        });
        keyNameField.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                keyNameFieldPropertyChange(evt);
            }
        });

        onDeleteCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "NO ACTION", "CASCADE", "SET NULL", "RESTRICT" }));

        onUpdateCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "NO ACTION", "CASCADE", "SET NULL", "RESTRICT" }));

        referencedCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                referencedComboActionPerformed(evt);
            }
        });
        referencedCombo.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                referencedComboPropertyChange(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(jButton3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton4)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton2))
                        .addComponent(jLabel6)))
                .addGap(8, 8, 8)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(refenrecedColumnCombo, 0, 145, Short.MAX_VALUE)
                    .addComponent(onDeleteCombo, 0, 145, Short.MAX_VALUE)
                    .addComponent(onUpdateCombo, 0, 145, Short.MAX_VALUE)
                    .addComponent(keyNameField, javax.swing.GroupLayout.DEFAULT_SIZE, 145, Short.MAX_VALUE)
                    .addComponent(columnCombo, 0, 145, Short.MAX_VALUE)
                    .addComponent(referencedCombo, 0, 145, Short.MAX_VALUE))
                .addGap(48, 48, 48))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton3)
                            .addComponent(jButton4)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(keyNameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(onDeleteCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(onUpdateCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(columnCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(20, 20, 20)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(referencedCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(refenrecedColumnCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 67, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addGap(24, 24, 24))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void fkListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_fkListValueChanged
        // TODO add your handling code here:
        Vector fkdata=new Vector();
        fkdata=(Vector) this.fkdatatable.get(this.fkList.getSelectedIndex());
        this.keyNameField.setText((String) fkdata.get(0));
        this.onDeleteCombo.setSelectedIndex((Integer) fkdata.get(1));
        this.onUpdateCombo.setSelectedIndex((Integer) fkdata.get(2));
        this.columnCombo.setSelectedIndex((Integer) fkdata.get(3));
        this.referencedCombo.setSelectedIndex((Integer) fkdata.get(4));
        this.refenrecedColumnCombo.setSelectedIndex((Integer) fkdata.get(5));
    }//GEN-LAST:event_fkListValueChanged

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        String fkname="fk_constraint_" + Integer.toString(this.fkList.getModel().getSize());
        Vector fkdata=new Vector();
        fkdata.add(fkname);
        for (int i=0;i<5;i++)
            fkdata.add(0);
        this.fkdatatable.add(fkdata);
        this.fkList.setSelectedIndex(this.fkList.getModel().getSize()-1);
    
        fkdata=new Vector();
        for (int i=0;i<this.fkdatatable.size();i++)
        {
            Vector a=new Vector();
            a=(Vector) this.fkdatatable.get(i);
            fkdata.add((String)a.get(0));
        }
        this.fkList.setListData(fkdata);
    }//GEN-LAST:event_jButton3ActionPerformed

    private void referencedComboPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_referencedComboPropertyChange
        // TODO add your handling code here:
        if (this.referencedCombo.getSelectedIndex()!=-1)
        {
            d.query("select * from " + (String) this.referencedCombo.getSelectedItem() + ";");
            this.refenrecedColumnCombo.setModel(new DefaultComboBoxModel(d.getColumnNames()));
        }
        
    }//GEN-LAST:event_referencedComboPropertyChange

    private void referencedComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_referencedComboActionPerformed
        // TODO add your handling code here:
        if (this.referencedCombo.getSelectedIndex()!=-1)
        {
            d.query("select * from " + (String) this.referencedCombo.getSelectedItem() + ";");
            this.refenrecedColumnCombo.setModel(new DefaultComboBoxModel(d.getColumnNames()));
        }
    }//GEN-LAST:event_referencedComboActionPerformed

    private void keyNameFieldPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_keyNameFieldPropertyChange
        // TODO add your handling code here:
    
    }//GEN-LAST:event_keyNameFieldPropertyChange

    private void keyNameFieldCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_keyNameFieldCaretUpdate
        // TODO add your handling code here:
        if (this.fkList.getSelectedIndex()!=-1)
        {
            Vector v=new Vector();
            v=(Vector) this.fkdatatable.get(this.fkList.getSelectedIndex());
            v.set(0, this.keyNameField.getText());
            this.fkdatatable.set(this.fkList.getSelectedIndex(), v);

            v=new Vector();

            for (int i=0;i<this.fkdatatable.size();i++)
            {
                Vector v1=new Vector();
                v1=(Vector) this.fkdatatable.get(i);
                v.add(v1.get(0));
            }
        
        this.fkList.setListData(v);
        }
    }//GEN-LAST:event_keyNameFieldCaretUpdate

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                ForeignKeyDialog dialog = new ForeignKeyDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox columnCombo;
    private javax.swing.JList fkList;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField keyNameField;
    private javax.swing.JComboBox onDeleteCombo;
    private javax.swing.JComboBox onUpdateCombo;
    private javax.swing.JComboBox refenrecedColumnCombo;
    private javax.swing.JComboBox referencedCombo;
    // End of variables declaration//GEN-END:variables

}
